﻿using MediatR;
using Message.Domain.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using WMSmessage.Api.Applcation.Command;
using WMSmessage.Api.Dtos;

namespace WMSmessage.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MessageCreateController : ControllerBase
    {
        protected readonly IMediator _mediator;

        public MessageCreateController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 新增消息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public async Task<APiResault<int>> CreateMessage(AddMessageDto a)
        {
            APiResault<int> resault = new APiResault<int>();
            try
            {
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                a.Office_Id = sid;
                var name = HttpContext.User.FindFirst(ClaimTypes.Name)?.Value;//获取用户名
                a.AddName = name;
                resault.Data = await _mediator.Send(new MessageCreateCommand() { dtos = a });
            }
            catch (Exception)
            {
                resault.Code = 500;
                resault.Message = "创建失败";
                throw;
            }
            return resault;
        }
    }
}
